#include "Property.h"
#include "Player.h"
#include <stdio.h>
#include <Windows.h>
#include <conio.h>

Property::Property(char* a_name, int a_rent[RENT_SIZE], int a_mortgageValue, int a_pricePerHouse, char a_color)
{
	m_name = a_name;
	for(int i = 0; i < RENT_SIZE; i++)
		m_rent[i] = a_rent[i];

	m_mortgageValue = a_mortgageValue;
	m_pricePerHouse = a_pricePerHouse;
	m_color = a_color;
	m_houses = 0;
	m_owner = 0;
	m_mortgaged = false;
}

void Property::Draw(int a_x, int a_y)
{
	const int width = 20;
	const int height = 13;
	const int clr = 6; // Clear a small area
	int tempColor;

	// Draw the box around the outside
	for(int y = 0; y < height; y++)
	{
		// Left line
		gotoXY(a_x, a_y+y);
		_putch('|');

		// Right line
		gotoXY(a_x+width, a_y+y);
		_putch('|');

	}

	// Top line and arrows
	gotoXY(a_x, a_y);
	SetColor(FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_INTENSITY);
	_putch('<');
	SetColor(STD_COLOR);
	printf("-----Inventory-----");
	SetColor(FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_INTENSITY);
	_putch('>');
	SetColor(STD_COLOR);

	// Bottom line
	gotoXY(a_x, a_y+height);
	printf("---------------------");

	// Set the background color
	switch(m_color)
	{
	case PURPLE:
		tempColor = BACKGROUND_RED|BACKGROUND_BLUE|BACKGROUND_INTENSITY;
		break;
	case LIGHT_BLUE:
		tempColor = BACKGROUND_BLUE|BACKGROUND_GREEN|BACKGROUND_INTENSITY;
		break;
	case RED:
		tempColor = BACKGROUND_RED|BACKGROUND_INTENSITY;
		break;
	case DARK_GREEN:
		tempColor = BACKGROUND_GREEN|STD_COLOR;
		break;
	case DARK_RED:
		tempColor = BACKGROUND_RED|STD_COLOR;
		break;
	case YELLOW:
		tempColor = BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_INTENSITY;
		break;
	case GREEN:
		tempColor = BACKGROUND_GREEN|BACKGROUND_INTENSITY;
		break;
	case DARK_BLUE:
		tempColor = BACKGROUND_BLUE|STD_COLOR;
		break;
	case UTILITY:
	case TRAIN:
		tempColor = STD_COLOR;
		break;
	}

	// Print the name
	SetColor(tempColor);
	gotoXY(++a_x, ++a_y);
	printf("%-*s", 19, m_name);
	SetColor(STD_COLOR);

	// Print the rent
	gotoXY(a_x, ++a_y);
	printf("Rent: $%-*d", clr, m_rent[0]);
	gotoXY(a_x, ++a_y);
	printf("1 house: $%-*d", clr, m_rent[1]);
	gotoXY(a_x, ++a_y);
	printf("2 houses: $%-*d", clr, m_rent[2]);
	gotoXY(a_x, ++a_y);
	printf("3 houses: $%-*d", clr, m_rent[3]);
	gotoXY(a_x, ++a_y);
	printf("4 houses: $%-*d", clr, m_rent[4]);
	gotoXY(a_x, ++a_y);
	printf("Hotel: $%-*d", clr, m_rent[5]);

	// Print the mortgage
	gotoXY(a_x, ++a_y);
	printf("Mortgage: $%-*d", clr, m_mortgageValue);
	gotoXY(a_x, ++a_y);
	printf("Houses cost $%-*d", clr, m_pricePerHouse);
}

char* Property::getName() { return m_name; }

int Property::getRent(int a_roll)
{
	if(m_mortgaged)
		return 0;
	if(!m_owner)
		return 0;

	// Rent of a utility
	if(m_color == UTILITY)
	{
		// If we own both, we get 10x the roll
		if(m_owner->getColorsOwned(UTILITY) == 2)
			return 10 * a_roll;
		else
			return 4 * a_roll;
	}
	// Rent of a train station
	else if(m_color == TRAIN)
	{
		// 50 * however many trains we have
		return m_rent[0]*m_owner->getColorsOwned(TRAIN);
	}
	return m_rent[m_houses];
}

// Get the number of houses on a property
// Returns -1 on non-housable properties
int Property::getHouses()
{
	if(m_color == TRAIN || m_color == UTILITY)
		return -1;

	return m_houses;
}

int Property::getOwner()
{ 	
	if(m_owner)
		return m_owner->getPlayerNum(); 
	else
		return -1;
}

void Property::setOwner(Player* a_owner) { m_owner = a_owner; }
